package com.shengruan.common;

import com.shengruan.admin.vo.User;
import com.shengruan.framework.action.ActionSupport;
import com.shengruan.servlet.ServletUtils;

public class BaseAction extends ActionSupport{
	
	/***
	 * 获得当前登录用户
	 */
	public User getCurrentUser()
	{
		User user = (User)this.request.getSession().getAttribute(Constants.USER);
		return user;
	}

    /**
     *
     */
    public void saveToken() {
        //生成新令牌
       String token =  ServletUtils.generateToken(request);
       request.setAttribute(Constants.CLIENT_TOKEN, token);
       //替换旧令牌
       this.setSession(Constants.TOKEN, token);
    }

    /**
     * 表单重复提交
     */
    public boolean isTokenValid() throws Exception{
        String clientToken = request.getParameter(Constants.CLIENT_TOKEN);
        String sessionToken = (String) request.getSession().getAttribute(Constants.TOKEN);
        if (sessionToken != null && !clientToken.equals(sessionToken)) {
            //表单重复提交
            saveToken();
            System.out.println("请不要重复提交！");
            this.setRequest(Constants.MESSAGE, "请不要重复提交！");
            return true;
        } else {
            // 表单不是重复提交
            saveToken();
            //正常的操作
            return false;
        }
    }

}
